$(function () {
    init();
    var productId = $("#productId").val();
    getData(productId);
});
var cprefix = "/home/compare"
var prefix = "/home/productBrowsing"
var dateHint;
//初始化预览日期
function initDate(ncFileDate) {
    var markStr = "{";
    for (var i = 0; i < ncFileDate.length; i++) {
        if (i == 0) {
            markStr += "'" + ncFileDate[i] + "':'" + ncFileDate[i].substring(8, 10) + "'";
        }
        else {
            markStr += ",'" + ncFileDate[i] + "':'" + ncFileDate[i].substring(8, 10) + "'";
        }
    }
    markStr += '}';
    var mark = eval("(" + markStr + ")");
    //时间可以选择
    $("#date").attr("disabled", false);
    if (dateHint) {
        dateHint.config.mark = mark;
        laydate.render({
            mark: mark
        })
    } else {
        //场 比测日期
        dateHint = laydate.render({
            elem: '#date'
            , showBottom: false
            , trigger: 'click'
            , min: -180
            , max: 0
            , mark: mark
            , done: function (value, date, endDate) {
                var productId = $("#productId").val();
                $.ajax({
                    type: "post",
                    async: true,
                    url: cprefix+"/getNcfile",
                    data: {
                        "productId": productId,
                        "date": value
                    },
                    dataType: "json",        //返回数据形式为json
                    success: function (res) {
                        var result = res.data;
                        var yulanNcfile = $("#yulanNcfile");
                        yulanNcfile.empty();
                        if (result.length > 0) {
                            for (var i = 0; i < result.length; i++) {
                                var option = "<option value = '" + result[i].id + "'>" + result[i].fileName + "</option>";
                                yulanNcfile.append(option);
                            }
                        } else {
                            var option = "<option value = ''>无</option>";
                            yulanNcfile.append(option);
                        }
                    }
                });
            }
        });
    }
}

function findNcFile() {
    $.ajax({
        type: "post",
        async: true,
        url: cprefix+"/getNcfile",
        data: {
            "productId": $("#productId").val(),
            "date": $("#date").val()
        },
        dataType: "json",        //返回数据形式为json
        success: function (res) {
            var result = res.data
            var yulanNcfile = $("#yulanNcfile");
            yulanNcfile.empty();
            if (result.length > 0) {
                for (var i = 0; i < result.length; i++) {
                    var option = "<option value = '" + result[i].id + "'>" + result[i].fileName + "</option>";
                    yulanNcfile.append(option);
                }
            } else {
                var option = "<option value = ''>无</option>";
                yulanNcfile.append(option);
            }
        }
    });
}

function init() {
    //sliderInit
    $('#longSlider').jRange({
        from: -90,
        to: 90,
        step: 1,
        format: '%s',
        width: 300,
        showLabels: true,
        isRange: true
    });

    $('#latiSlider').jRange({
        from: -90,
        to: 90,
        step: 1,
        format: '%s',
        width: 300,
        showLabels: true,
        isRange: true
    });

    //checkBoxInit
    $(".checkbox").toggle(
        function () {
            $(".speedbox").slideToggle(400);
            $(this).addClass("checkboxactive")
        }, function () {
            $(".speedbox").slideToggle(400);
            $(this).removeClass("checkboxactive")
        });
    $('.collapse.in').prev('.panel-heading').addClass('active');
    $('#accordion, #bs-collapse').on('show.bs.collapse', function (a) {
        $(a.target).prev('.panel-heading').addClass('active');
    }).on('hide.bs.collapse', function (a) {
        $(a.target).prev('.panel-heading').removeClass('active');
    });

};

var elementsdb = [];
function getData(productId) {
    //时间禁止选择
    $("#date").attr("disabled", true);
    $.ajax({
        type: "post",
        async: true,
        url: prefix+"/getPreviewData",
        data: {
            "productId": productId
        },
        dataType: "json",        //返回数据形式为json
        success: function (res) {
            var result = res.data
            var data = result.param;
            initDate(result.ncFileDate);
            for (var i = 0; i < data.length; i++) {
                var value = data[i];
                if (value.isOutParameter == 0 && value.elementZh == "时间") {
                    $("#timeDiv").css("display", "");
                    $("#timeLabel").css("display", "");
                    $("#timeSelect").css("display", "");
                    var realValues = value.realValue.split(",");
                    for (var j = 0; j < realValues.length; j++) {
                        var realValue = realValues[j];
                        $("#timeSelect").append("<option value='" + realValue + "'>" + realValue + "</option>");
                    }
                    elementsdb.push(value.elementZh + ";" + value.elementEn);
                }
                if (value.isOutParameter == 1) {
                    $("#out").append("<option value='" + value.elementEn + "'>" + value.elementEn + "," + value.elementZh + "</option>");
                    $("#out2").append("<option value='" + value.elementEn + "'>" + value.elementEn + "," + value.elementZh + "</option>");
                }
            }
        }
    });
}

function doPreview() {
    var ncFileId = $("#yulanNcfile").val();
    if (!ncFileId) {
        alert("预览文件不能为空");
        return ;
    }

    var type = $("#type").val();
    var productId = $("#productId").val();
    var date = $("#date").val();

    // 先查看预览的文件是否存在，不存在直接提示不存在
    $.ajax({
        type: "post",
        async: true,
        url: prefix+"/isExitLocalFile",
        data: {
            "ncFileId":ncFileId
        },
        dataType: "json",        //返回数据形式为json
        success: function (res) {
            var result = res.data;
            var flag = result.flag;
            if(!flag){ // 未找到文件，不能进行预览，返回
            	alert("未找到服务器中存储的文件，请确认文件在服务器中是否真的存在 ！");
            	return ;
            }else{
            	// 文件存在，进行预览操作
            	previewFile();
            }
        }
    });


    function previewFile(){
    	var elementDtos = new Array();
        for (var i in elementsdb) {
            var elements = elementsdb[i].split(";");
            var element = elements[0];
            var elementEn = elements[1];

            if (element == "时间") {
                var time = $("#timeSelect").val();
                elementDto = {
                    element: elementEn,
                    minValue: time,
                    maxValue: time,
                    flag: "-1"
                };
            }
            elementDtos.push(elementDto);
        }
        var yaosu = $("#out").val();
        var yaosu2 = "";
        if ($("#out2Div").css("display") != "none") {
            yaosu2 = $("#out2").val();
        }

        var url = prefix+"/resultother?productId="+productId+"&fileId="+ncFileId;
    	layer.open({
            type: 2,
            title:"预览结果",
            area: ['80%', '90%'],
            shadeClose: false, //点击遮罩关闭
            shade: 0.3,
            content: url,
            btn: ['关闭'],
            scrollbar: false,
            yes: function(index){
            	 layer.close(index);
            	 layer.closeAll(); //疯狂模式，关闭所有层
            },
            cancel: function(index, layero){
        	    layer.close(index);
        	    layer.closeAll(); //疯狂模式，关闭所有层
        	}
        });
    }

}

